package servlet;

import dao.CustomerDao;
import dao.CustomerDaoImpl;
import entity.Customer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/doUpdateServlet")
public class DoUpdateServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");
        String gender = req.getParameter("gender");
        int  age = Integer.parseInt(req.getParameter("age"));
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");

        Customer customer = new Customer();

        customer.setId(id);
        customer.setName(name);
        customer.setGender(gender);
        customer.setAge(age);
        customer.setPhone(phone);
        customer.setEmail(email);

        CustomerDao customerDao = new CustomerDaoImpl();
        int i = customerDao.updateCustomer(customer);
        req.getRequestDispatcher("findAllServlet.java").forward(req,resp);
    }
}
